<template>
  <div class="auth-container">
    <div class="auth-card card">
      <h1 class="auth-title">登录</h1>
      
      <form @submit.prevent="handleLogin" class="auth-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            id="username" 
            v-model="form.username" 
            type="text" 
            placeholder="请输入用户名" 
            required
          >
        </div>

        <div class="form-group">
          <label for="password">密码</label>
          <div class="password-input">
            <input 
              id="password" 
              v-model="form.password" 
              :type="showPassword ? 'text' : 'password'" 
              placeholder="请输入密码" 
              required
            >
            <button 
              type="button" 
              class="toggle-password" 
              @click="showPassword = !showPassword"
            >
              {{ showPassword ? '隐藏' : '显示' }}
            </button>
          </div>
        </div>

        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="form.remember">
            <span>记住我</span>
          </label>
          <a href="/auth/forgot-password" class="forgot-password">忘记密码？</a>
        </div>

        <button 
          type="submit" 
          class="btn btn-primary submit-btn"
          :disabled="isLoading"
        >
          {{ isLoading ? '登录中...' : '登录' }}
        </button>
      </form>

      <div class="auth-divider">
        <span>或</span>
      </div>

      <div class="social-login">
        <button class="btn btn-social github">
          <i class="icon-github"></i>
          使用 GitHub 登录
        </button>
        <button class="btn btn-social google">
          <i class="icon-google"></i>
          使用 Google 登录
        </button>
      </div>

      <div class="auth-footer">
        还没有账号？<a href="/auth/register">立即注册</a>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useStore } from '~/composables/useStore'
import { useRouter } from 'vue-router'

const router = useRouter()
const { login, addNotification, setLoading } = useStore()

const form = ref({
  username: '',
  password: '',
  remember: false
})

const showPassword = ref(false)
const isLoading = ref(false)

const handleLogin = async () => {
  try {
    setLoading(true)
    isLoading.value = true
    
    const success = await login(form.value.username, form.value.password)
    
    if (success) {
      addNotification('success', '登录成功')
      router.push('/')
    } else {
      addNotification('error', '用户名或密码错误')
    }
  } catch (error) {
    console.error('登录失败:', error)
    addNotification('error', '登录失败，请稍后重试')
  } finally {
    setLoading(false)
    isLoading.value = false
  }
}
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
  background-color: var(--bg-secondary);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  padding: 32px;
}

.auth-title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 24px;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  font-size: 14px;
  color: var(--text-secondary);
}

.form-group input {
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 14px;
}

.password-input {
  position: relative;
}

.toggle-password {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.forgot-password {
  color: var(--primary-color);
  text-decoration: none;
}

.submit-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  margin-top: 8px;
}

.auth-divider {
  display: flex;
  align-items: center;
  margin: 24px 0;
  color: var(--text-secondary);
}

.auth-divider::before,
.auth-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background-color: var(--border-color);
}

.auth-divider span {
  padding: 0 16px;
}

.social-login {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.btn-social {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border-color);
  background: white;
  color: var(--text-primary);
}

.btn-social.github {
  background-color: #24292e;
  color: white;
  border-color: #24292e;
}

.btn-social.google {
  background-color: #fff;
  color: #757575;
  border-color: #ddd;
}

.auth-footer {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
  color: var(--text-secondary);
}

.auth-footer a {
  color: var(--primary-color);
  text-decoration: none;
}

@media (max-width: 480px) {
  .auth-card {
    padding: 24px;
  }
}
</style> 